package cn.com.egova.bouncingmenu;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

/**
 * Created by yuanchao on 2017/4/16.
 */

public class BouncingMenu{
    private Context mContext;
    private ViewGroup mParentVG;
    private ViewGroup mMenuLayout;
    private BouncingView mBouncingView;

    public BouncingMenu(Activity activity, ViewGroup viewGroup) {
        this(activity,viewGroup,null);
    }

    public BouncingMenu(Activity activity, ViewGroup viewGroup,BouncingView.OnStatusChangeListener onStatusChangeListener) {
        this(activity,viewGroup,null,0);
    }

    public BouncingMenu(Activity activity, ViewGroup viewGroup,BouncingView.OnStatusChangeListener onStatusChangeListener,int delayTimeMillis) {
        this.mContext = activity;
        this.mMenuLayout = viewGroup;

        //找到根容器.
        mParentVG = findContentParent(activity);
        //找到有特效的BouncingView.
        View view = mMenuLayout.getChildAt(0);
        if(view!=null && view instanceof BouncingMenuLayout){
            BouncingMenuLayout layout = (BouncingMenuLayout) view;
            mBouncingView = (BouncingView) layout.getChildAt(0);
            mBouncingView.setDelayTimeMillis(delayTimeMillis); //设置视差时间.
            mBouncingView.setOnStatusChangeListener(onStatusChangeListener);
        }
    }


    public static BouncingMenu makeMenu(Activity activity, int resId) {
        return makeMenu(activity,resId,null);
    }

    public static BouncingMenu makeMenu(Activity activity, int resId,BouncingView.OnStatusChangeListener onStatusChangeListener) {
        return makeMenu(activity,resId,null,0);
    }

    public static BouncingMenu makeMenu(Activity activity, int resId,BouncingView.OnStatusChangeListener onStatusChangeListener,int delayTimeMillis) {
        ViewGroup viewGroup = (ViewGroup) LayoutInflater.from(activity).inflate(resId, null, false);
        return makeMenu(activity,viewGroup,onStatusChangeListener,delayTimeMillis);
    }

    public static BouncingMenu makeMenu(Activity activity, ViewGroup viewGroup){
        return makeMenu(activity,viewGroup,null);
    }

    public static BouncingMenu makeMenu(Activity activity, ViewGroup viewGroup, BouncingView.OnStatusChangeListener onStatusChangeListener){
        return makeMenu(activity,viewGroup,onStatusChangeListener,0);
    }

    /**
     * 不管是几个参数的makeMenu方法,最终都会调到这里来.
     * @param activity
     * @param viewGroup
     * @param onStatusChangeListener
     * @param delayTimeMillis
     * @return
     */
    public static BouncingMenu makeMenu(Activity activity, ViewGroup viewGroup, BouncingView.OnStatusChangeListener onStatusChangeListener,int delayTimeMillis){
        BouncingMenu bouncingMenu = new BouncingMenu(activity,viewGroup,onStatusChangeListener,delayTimeMillis);
        return bouncingMenu;
    }

    /**
     * 将弹出菜单布局添加到容器中显示到前台,注意添加的其实是整个mMenuLayout,而不只是底部的菜单部分,
     * 只不过由于mMenuLayout是inflate进来,然后add到content中的,并且没有设置backGround,所以是透明的,
     * 我们能透过它看到activity的其他控件,并且我们点击mMenuLayout的透明区域时,如果是点在按钮上,
     * 是会相应点击事件的,因为我们并未拦截.
     * 总结:在同一个Activity中inflate进来的layout布局,如果没有给它设置背景颜色,则默认是透明的.
     * @return
     */
    public BouncingMenu show() {
        if (mMenuLayout.getParent() != null) {
            mParentVG.removeView(mMenuLayout);
        }
        if(mBouncingView==null){
            Toast.makeText(mContext,"弹出菜单的布局不正确",Toast.LENGTH_SHORT).show();
            return this;
        }
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mParentVG.addView(mMenuLayout, lp);
        mBouncingView.show();
        return this;
    }


    public BouncingMenu dismiss(){
        if(mMenuLayout!=null && mMenuLayout.getParent()!=null){
            mParentVG.removeView(mMenuLayout);
            mMenuLayout = null;
        }
        return this;
    }


    /**
     * android.R.id.content 实际上是一个FrameLayout.
     *
     * @param activity
     * @return
     */
    private ViewGroup findContentParent(Activity activity) {
        View decorView = activity.getWindow().getDecorView();
        ViewGroup viewGroup = (ViewGroup) decorView.findViewById(android.R.id.content);
        return viewGroup;
    }
}
